package com.keller.controller;

import com.keller.domian.Product;
import com.keller.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @RequestMapping("/findAll")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Product> listProduct = productService.findAll();
        System.out.println(listProduct);
        mv.addObject("productList",listProduct);
        System.out.println(listProduct);
        mv.setViewName("product-list");
        return mv;
    }

    @RequestMapping("/save")
    public String saveProduct(Product product){
        productService.saveProduct(product);
        return "redirect:/product/findAll";
    }

}
